/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     uiCounter.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "uiCounter.h"
#include "bsp.h"
#include "levetop.h"
#include "data.h"
#include "rtc.h"
#include "keyBoard.h"

Count_Info gCount_Info[COUNT_SIZE];
Record_count_Info gRecord_count_Info[20];
uint8_t Count_sum = 0;
uint8_t Count_flag = 0;
uint8_t Record_count = 0;
uint16_t Count_reg[8];
uint8_t Count_reg_num;

void show_Count(void)
{
	if (Count_sum > 0 && Count_flag == 1)
	{
		Count_flag = 0;
		for (uint8_t i = 0; i < Count_sum; i++)
		{
			if (((var[gCount_Info[i].c_add * 2] << 8) + var[gCount_Info[i].c_add * 2 + 1]) != 0)
				Display_Count(i);
			else if (((var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1]) !=
					 (var_temp[gCount_Info[i].t_add * 2] << 8) + var_temp[gCount_Info[i].t_add * 2 + 1])
			{
				Display_Count(i);
				var_temp[gCount_Info[i].t_add * 2] = var[gCount_Info[i].t_add * 2];
				var_temp[gCount_Info[i].t_add * 2 + 1] = var[gCount_Info[i].t_add * 2 + 1];
			}
		}
	}
}

/***********************************************************************************
* Function Name	: count_display
* Description	: Get time counter info.
* Input         : - type: time counter info sources
				  - buff: time counter info
* Output        : None
* Return        : None
************************************************************************************/
void count_display(uint8_t type, uint8_t *buff)
{
	uint8_t i = 0;
	uint16_t pointer = 0;

	if (type == 0)
	{
		gCount_Info[Count_sum].v_add = (buff[5] + (buff[6] << 8));
		gCount_Info[Count_sum].t_add = buff[7] + (buff[8] << 8);
		gCount_Info[Count_sum].c_add = buff[9] + (buff[10] << 8);
		gCount_Info[Count_sum].x = buff[11] + (buff[12] << 8);
		gCount_Info[Count_sum].y = buff[13] + (buff[14] << 8);
		gCount_Info[Count_sum].ID_start = buff[15] + (buff[16] << 8);
		gCount_Info[Count_sum].dis_mode = buff[17];
		gCount_Info[Count_sum].dir = buff[18];
		gCount_Info[Count_sum].end_mode = buff[19];
		gCount_Info[Count_sum].end_feedback = buff[20];
		gCount_Info[Count_sum].var_addr[0] = buff[21] + (buff[22] << 8);
		gCount_Info[Count_sum].var[0] = buff[23] + (buff[24] << 8);
		gCount_Info[Count_sum].var_addr[1] = buff[25] + (buff[26] << 8);
		gCount_Info[Count_sum].var[1] = buff[27] + (buff[28] << 8);
		gCount_Info[Count_sum].var_addr[2] = buff[29] + (buff[30] << 8);
		gCount_Info[Count_sum].var[2] = buff[31] + (buff[32] << 8);
		gCount_Info[Count_sum].var_addr[3] = buff[33] + (buff[34] << 8);
		gCount_Info[Count_sum].var[3] = buff[35] + (buff[36] << 8);
		gCount_Info[Count_sum].var_addr[4] = buff[37] + (buff[38] << 8);
		gCount_Info[Count_sum].var[4] = buff[39] + (buff[40] << 8);
		gCount_Info[Count_sum].var_addr[5] = buff[41] + (buff[42] << 8);
		gCount_Info[Count_sum].var[5] = buff[43] + (buff[44] << 8);
		gCount_Info[Count_sum].var_addr[6] = buff[45] + (buff[46] << 8);
		gCount_Info[Count_sum].var[6] = buff[47] + (buff[48] << 8);
		gCount_Info[Count_sum].var_addr[7] = buff[49] + (buff[50] << 8);
		gCount_Info[Count_sum].var[7] = buff[51] + (buff[52] << 8);
	}
	else if (type == 1)
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		gCount_Info[Count_sum].v_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		gCount_Info[Count_sum].t_add = var[pointer + 3] + (var[pointer + 4] << 8);
		gCount_Info[Count_sum].c_add = var[pointer + 5] + (var[pointer + 6] << 8);
		gCount_Info[Count_sum].x = var[pointer + 7] + (var[pointer + 8] << 8);
		gCount_Info[Count_sum].y = var[pointer + 9] + (var[pointer + 10] << 8);
		gCount_Info[Count_sum].ID_start = var[pointer + 11] + (var[pointer + 12] << 8);
		gCount_Info[Count_sum].dis_mode = var[pointer + 13];
		gCount_Info[Count_sum].dir = var[pointer + 14];
		gCount_Info[Count_sum].end_mode = var[pointer + 15];
		gCount_Info[Count_sum].end_feedback = var[pointer + 16];
		gCount_Info[Count_sum].var_addr[0] = var[pointer + 17] + (var[pointer + 18] << 8);
		gCount_Info[Count_sum].var[0] = var[pointer + 19] + (var[pointer + 20] << 8);
		gCount_Info[Count_sum].var_addr[1] = var[pointer + 21] + (var[pointer + 22] << 8);
		gCount_Info[Count_sum].var[1] = var[pointer + 23] + (var[pointer + 24] << 8);
		gCount_Info[Count_sum].var_addr[2] = var[pointer + 25] + (var[pointer + 26] << 8);
		gCount_Info[Count_sum].var[2] = var[pointer + 27] + (var[pointer + 28] << 8);
		gCount_Info[Count_sum].var_addr[3] = var[pointer + 29] + (var[pointer + 30] << 8);
		gCount_Info[Count_sum].var[3] = var[pointer + 31] + (var[pointer + 32] << 8);
		gCount_Info[Count_sum].var_addr[4] = var[pointer + 33] + (var[pointer + 34] << 8);
		gCount_Info[Count_sum].var[4] = var[pointer + 35] + (var[pointer + 36] << 8);
		gCount_Info[Count_sum].var_addr[5] = var[pointer + 37] + (var[pointer + 38] << 8);
		gCount_Info[Count_sum].var[5] = var[pointer + 39] + (var[pointer + 40] << 8);
		gCount_Info[Count_sum].var_addr[6] = var[pointer + 41] + (var[pointer + 42] << 8);
		gCount_Info[Count_sum].var[6] = var[pointer + 43] + (var[pointer + 44] << 8);
		gCount_Info[Count_sum].var_addr[7] = var[pointer + 45] + (var[pointer + 46] << 8);
		gCount_Info[Count_sum].var[7] = var[pointer + 47] + (var[pointer + 48] << 8);
	}

	for(uint8_t i=0;i<8;i++)
		Var_AddrRemapping(&gCount_Info[Count_sum].var_addr[i]);

	if (Pop_up_flag == 1) // Popup offset
	{
		gCount_Info[Count_sum].x += Pop_up_x;
		gCount_Info[Count_sum].y += Pop_up_y;
	}
	for (i = 0; i < Record_count; i++)
	{
		if (gCount_Info[Count_sum].v_add == gRecord_count_Info[i].v_add)
		{
			gRecord_count_Info[i].v_add = gRecord_count_Info[Record_count - 1].v_add;
			gRecord_count_Info[i].t_add = gRecord_count_Info[Record_count - 1].t_add;
			gRecord_count_Info[i].c_add = gRecord_count_Info[Record_count - 1].c_add;
			gRecord_count_Info[i].dir = gRecord_count_Info[Record_count - 1].dir;
			gRecord_count_Info[i].end_feedback = gRecord_count_Info[Record_count - 1].end_feedback;
			gRecord_count_Info[i].var[0] = gRecord_count_Info[Record_count - 1].var[0];
			gRecord_count_Info[i].var_addr[0] = gRecord_count_Info[Record_count - 1].var_addr[0];
			gRecord_count_Info[i].var[1] = gRecord_count_Info[Record_count - 1].var[1];
			gRecord_count_Info[i].var_addr[1] = gRecord_count_Info[Record_count - 1].var_addr[1];
			gRecord_count_Info[i].var[2] = gRecord_count_Info[Record_count - 1].var[2];
			gRecord_count_Info[i].var_addr[2] = gRecord_count_Info[Record_count - 1].var_addr[2];
			gRecord_count_Info[i].var[3] = gRecord_count_Info[Record_count - 1].var[3];
			gRecord_count_Info[i].var_addr[3] = gRecord_count_Info[Record_count - 1].var_addr[3];
			gRecord_count_Info[i].var[4] = gRecord_count_Info[Record_count - 1].var[4];
			gRecord_count_Info[i].var_addr[4] = gRecord_count_Info[Record_count - 1].var_addr[4];
			gRecord_count_Info[i].var[5] = gRecord_count_Info[Record_count - 1].var[5];
			gRecord_count_Info[i].var_addr[5] = gRecord_count_Info[Record_count - 1].var_addr[5];
			gRecord_count_Info[i].var[6] = gRecord_count_Info[Record_count - 1].var[6];
			gRecord_count_Info[i].var_addr[6] = gRecord_count_Info[Record_count - 1].var_addr[6];
			gRecord_count_Info[i].var[7] = gRecord_count_Info[Record_count - 1].var[7];
			gRecord_count_Info[i].var_addr[7] = gRecord_count_Info[Record_count - 1].var_addr[7];
			Record_count--;
		}
	}
	gCount_Info[Count_sum].last_w = 0;
	Display_Count(Count_sum);

	Count_sum++;
}

uint8_t Display_Count(uint8_t i)
{
	uint16_t ID[11];
	uint8_t buff[12] = {0};
	uint16_t temp;
	uint16_t x_time = 0;
	uint8_t j = 0, cnt = 0;
	uint16_t x_src, y_src, x_des, y_des, w_real, h_real,x_date = 0;

	if (((var[gCount_Info[i].c_add * 2] << 8) + var[gCount_Info[i].c_add * 2 + 1]) == 2)
	{
		if (gCount_Info[i].last_w > 0)
		{
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (gCount_Info[i].ID_start) + multi_language * 12 * addr_index[11], 12);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);

			if (Screen_DIR == 0)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
				x_src = gCount_Info[i].x;
				y_src = gCount_Info[i].y;
				w_real = gCount_Info[i].last_w;
				h_real = pic_h;
			}
			else if (Screen_DIR == 1)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
				x_src = gCount_Info[i].x  - pic_w;
				y_src = gCount_Info[i].y;
				w_real = pic_w;
				h_real = gCount_Info[i].last_w;
			}
			else if (Screen_DIR == 2)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this

				x_src = gCount_Info[i].x  - gCount_Info[i].last_w;
				y_src = gCount_Info[i].y - pic_h;
				w_real = gCount_Info[i].last_w;
				h_real = pic_h;
			}
			else if (Screen_DIR == 3)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
				x_src = gCount_Info[i].x;
				y_src = gCount_Info[i].y - gCount_Info[i].last_w;
				w_real = pic_w;
				h_real = gCount_Info[i].last_w;
			}

			LT268_dispaly_CutPicture(x_src, y_src,
					 x_src, y_src, w_real , h_real,
					 FullScreen_W, FullScreen_addr, FullScreen_color, FullScreen_flag);

			gCount_Info[i].last_w = 0;
		}
		return 0;
	}
	if (gCount_Info[i].dis_mode == 0)
	{
		return 0;
	}
	else if (gCount_Info[i].dis_mode == 1) // Hour, minute and second
	{
		temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
		ID[0] = temp / 36000 % 10;
		ID[1] = temp / 3600 % 10;
		ID[2] = 10;
		temp = temp % 3600;
		ID[3] = temp / 600 % 10;
		ID[4] = temp / 60 % 10;
		ID[5] = 10;
		temp = temp % 60;
		ID[6] = temp / 10 % 10;
		ID[7] = temp % 10;
		cnt = 7;
	}
	else if (gCount_Info[i].dis_mode == 2)
	{
		temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];

		ID[0] = temp / 36000 % 10;
		ID[1] = temp / 3600 % 10;
		ID[2] = 10;
		temp = temp % 3600;
		ID[3] = temp / 600 % 10;
		ID[4] = temp / 60 % 10;
		cnt = 4;
	}
	else if (gCount_Info[i].dis_mode == 3)
	{
		temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
		temp = temp % 3600;
		ID[0] = temp / 600 % 10;
		ID[1] = temp / 60 % 10;
		ID[2] = 10;
		temp = temp % 60;
		ID[3] = temp / 10 % 10;
		ID[4] = temp % 10;
		cnt = 4;
	}
	else if (gCount_Info[i].dis_mode == 4)
	{
		if (((var[gCount_Info[i].v_add * 2] << 8) + var[gCount_Info[i].v_add * 2 + 1]) > 10000)
		{
			temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
			ID[0] = temp / 10000 % 10;
			ID[1] = temp / 1000 % 10;
			ID[2] = temp / 100 % 10;
			ID[3] = temp / 10 % 10;
			ID[4] = temp % 10;
			cnt = 4;
		}
		else if (((var[gCount_Info[i].v_add * 2] << 8) + var[gCount_Info[i].v_add * 2 + 1]) > 1000)
		{
			temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
			ID[0] = temp / 1000 % 10;
			ID[1] = temp / 100 % 10;
			ID[2] = temp / 10 % 10;
			ID[3] = temp % 10;
			cnt = 3;
		}
		else if (((var[gCount_Info[i].v_add * 2] << 8) + var[gCount_Info[i].v_add * 2 + 1]) > 100)
		{
			temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
			ID[0] = temp / 100 % 10;
			ID[1] = temp / 10 % 10;
			ID[2] = temp % 10;
			cnt = 2;
		}
		else if (((var[gCount_Info[i].v_add * 2] << 8) + var[gCount_Info[i].v_add * 2 + 1]) > 10)
		{
			temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
			ID[0] = temp / 10 % 10;
			ID[1] = temp % 10;
			cnt = 1;
		}
		else
		{
			temp = (var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1];
			ID[0] = temp % 10;
			cnt = 0;
		}
	}
//	var_temp[gCount_Info[i].t_add * 2] = var[gCount_Info[i].t_add * 2];
//	var_temp[gCount_Info[i].t_add * 2 + 1] = var[gCount_Info[i].t_add * 2 + 1];

	/*On the keyboard input page*/
	if (DioKb_flag == 1)
	{
		if (!(gCount_Info[i].x > gDio_Info[Dio_num].Xe || (gCount_Info[i].x + x_time) < gDio_Info[Dio_num].Xs || gCount_Info[i].y > gDio_Info[Dio_num].Ye || (gCount_Info[i].y + pic_h) < gDio_Info[Dio_num].Ys))
		{
			return 0;
		}
	}
	else if (AioKb_flag == 1)
	{
		if (!(gCount_Info[i].x > gAio_Info[Aio_num].Xe || (gCount_Info[i].x + x_time) < gAio_Info[Aio_num].Xs || gCount_Info[i].y > gAio_Info[Aio_num].Ye || (gCount_Info[i].y + pic_h) < gAio_Info[Aio_num].Ys))
		{
			return 0;
		}
	}
	else if (GBKioKb_flag == 1)
	{
		if (!(gCount_Info[i].x > gGBKio_Info[GBKio_num].Xe || (gCount_Info[i].x + x_time) < gGBKio_Info[GBKio_num].Xs || gCount_Info[i].y > gGBKio_Info[GBKio_num].Ye || (gCount_Info[i].y + pic_h) < gGBKio_Info[GBKio_num].Ys))
		{
			return 0;
		}
	}

	x_date = 0;
	for (j = 0; j <= cnt; j++)
	{
		if (gCount_Info[i].ID_start + ID[j] >= addr_index[11])
			continue; // picture is available
		LT_ReadFlash_UI(buff, addr_index[10] + 12 * (gCount_Info[i].ID_start + ID[j]) + multi_language * 12 * addr_index[11], 12);

		pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
		pic_w = buff[4] + (buff[5] << 8);
		pic_h = buff[6] + (buff[7] << 8);
		pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
		pic_flag = buff[11];

		if (pic_len != 0 && pic_flag != 0xFF) // picture is not empty
		{
			if (Screen_DIR == 0)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this

				x_src = gCount_Info[i].x  +  x_date;
				y_src = gCount_Info[i].y;
				w_real = pic_w;
			}
			else if (Screen_DIR == 1)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this

				x_src = gCount_Info[i].x  - pic_w;
				y_src = gCount_Info[i].y + x_date;
				w_real = pic_h;

			}
			else if (Screen_DIR == 2)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this

				x_src = gCount_Info[i].x  - x_date - pic_w;
				y_src = gCount_Info[i].y - pic_h;
				w_real = pic_w;
			}
			else if (Screen_DIR == 3)
			{
				// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
				x_src = gCount_Info[i].x ;
				y_src = gCount_Info[i].y - pic_h - x_date;
				w_real = pic_h;
			}
			if (pic_flag == 0 || pic_flag == 1 || pic_flag == 2)
				LT_ShowPicture(x_src, y_src, pic_w, pic_h, pic_add, pic_len, pic_flag);
			x_date += w_real;
		}
	}
	gCount_Info[i].last_w = x_time;
}

void Saving_counting_parameters(void)
{
	uint8_t i, j;
	for (i = 0; i < Count_sum; i++)
	{
		if (Record_count > 0 && gCount_Info[i].end_mode == 1)
		{
			for (j = 0; j < Record_count; j++)
			{
				if (gCount_Info[i].v_add == gRecord_count_Info[j].v_add)
					break;
				if (j == Record_count - 1)
				{
					gRecord_count_Info[Record_count].v_add = gCount_Info[i].v_add;
					gRecord_count_Info[Record_count].t_add = gCount_Info[i].t_add;
					gRecord_count_Info[Record_count].c_add = gCount_Info[i].c_add;
					gRecord_count_Info[Record_count].dir = gCount_Info[i].dir;
					gRecord_count_Info[Record_count].end_feedback = gCount_Info[i].end_feedback;
					gRecord_count_Info[Record_count].var[0] = gCount_Info[i].var[0];
					gRecord_count_Info[Record_count].var_addr[0] = gCount_Info[i].var_addr[0];
					gRecord_count_Info[Record_count].var[1] = gCount_Info[i].var[1];
					gRecord_count_Info[Record_count].var_addr[1] = gCount_Info[i].var_addr[1];
					gRecord_count_Info[Record_count].var[2] = gCount_Info[i].var[2];
					gRecord_count_Info[Record_count].var_addr[2] = gCount_Info[i].var_addr[2];
					gRecord_count_Info[Record_count].var[3] = gCount_Info[i].var[3];
					gRecord_count_Info[Record_count].var_addr[3] = gCount_Info[i].var_addr[3];
					gRecord_count_Info[Record_count].var[4] = gCount_Info[i].var[4];
					gRecord_count_Info[Record_count].var_addr[4] = gCount_Info[i].var_addr[4];
					gRecord_count_Info[Record_count].var[5] = gCount_Info[i].var[5];
					gRecord_count_Info[Record_count].var_addr[5] = gCount_Info[i].var_addr[5];
					gRecord_count_Info[Record_count].var[6] = gCount_Info[i].var[6];
					gRecord_count_Info[Record_count].var_addr[6] = gCount_Info[i].var_addr[6];
					gRecord_count_Info[Record_count].var[7] = gCount_Info[i].var[7];
					gRecord_count_Info[Record_count].var_addr[7] = gCount_Info[i].var_addr[7];
					Record_count++;
				}
			}
		}
		else if (gCount_Info[i].end_mode == 1)
		{
			gRecord_count_Info[Record_count].v_add = gCount_Info[i].v_add;
			gRecord_count_Info[Record_count].t_add = gCount_Info[i].t_add;
			gRecord_count_Info[Record_count].c_add = gCount_Info[i].c_add;
			gRecord_count_Info[Record_count].dir = gCount_Info[i].dir;
			gRecord_count_Info[Record_count].end_feedback = gCount_Info[i].end_feedback;
			gRecord_count_Info[Record_count].var[0] = gCount_Info[i].var[0];
			gRecord_count_Info[Record_count].var_addr[0] = gCount_Info[i].var_addr[0];
			gRecord_count_Info[Record_count].var[1] = gCount_Info[i].var[1];
			gRecord_count_Info[Record_count].var_addr[1] = gCount_Info[i].var_addr[1];
			gRecord_count_Info[Record_count].var[2] = gCount_Info[i].var[2];
			gRecord_count_Info[Record_count].var_addr[2] = gCount_Info[i].var_addr[2];
			gRecord_count_Info[Record_count].var[3] = gCount_Info[i].var[3];
			gRecord_count_Info[Record_count].var_addr[3] = gCount_Info[i].var_addr[3];
			gRecord_count_Info[Record_count].var[4] = gCount_Info[i].var[4];
			gRecord_count_Info[Record_count].var_addr[4] = gCount_Info[i].var_addr[4];
			gRecord_count_Info[Record_count].var[5] = gCount_Info[i].var[5];
			gRecord_count_Info[Record_count].var_addr[5] = gCount_Info[i].var_addr[5];
			gRecord_count_Info[Record_count].var[6] = gCount_Info[i].var[6];
			gRecord_count_Info[Record_count].var_addr[6] = gCount_Info[i].var_addr[6];
			gRecord_count_Info[Record_count].var[7] = gCount_Info[i].var[7];
			gRecord_count_Info[Record_count].var_addr[7] = gCount_Info[i].var_addr[7];
			Record_count++;
		}
	}
}
